package cn.zjj.controller;

import cn.zjj.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
//@DefaultProperties(defaultFallback = "fallbackMethod")  全局熔断
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;


    @GetMapping("/getById/{id}")
    @HystrixCommand(fallbackMethod = "getByIdFallback") //熔断后返回的兜底数据,  降级方法名
//    @HystrixCommand // 标记方法熔断  全局熔断中,需要熔断的接口需要标记熔断
    public User getById(@PathVariable("id") Long id){
        //接口路径
        String url="http://user-server/user/getById/"+id;
        //restTemplate发送http请求
        return restTemplate.getForObject(url,User.class);
    }
    //如果是全局熔断,返回值需要统一,因为共用一个,所以最好用,jsonresult,需要放的参数放到resultObj,且不能有形参,因为共用不能统一形参
    public User getByIdFallback(@PathVariable("id") Long id){
        return new User(-1L,"触发熔断","服务不可用!");
    }
}
